package is.sclab;

import is.sclab.mail.MailImpl;
import is.sclab.mail.MessageBody;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class MailServlet extends HttpServlet{	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		
		String sender = req.getParameter("sender");
		String receiver = req.getParameter("receiver");
		if (sender == null || sender.equals("") || receiver == null|| receiver.equals("")) {
			resp.getWriter().println("insufficent inputs");
			return;
		}
		
		MailImpl mail = new MailImpl();
		MessageBody msg = new MessageBody.Builder(sender, receiver).Subject(req.getParameter("subject")).Text(req.getParameter("text")).Build();
		mail.send(msg);
		
		Writer out = resp.getWriter();
		output(out);
	}


	
	private static void output(Writer out)throws IOException{
		out.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
	    out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">");
	    out.write("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
	    out.write("<body>");
		out.write("<pre>MailToSend</pre>");
		out.write("<a href=\"/index.html\">top</a>");
	    out.write("</body></html>");
	}
}
